package com.snow.java.base.junit;

import com.snow.java.base.entity.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @类名称：StudentTest
 * @类描述：Java 1.8新特性，对集合的操作
 * @author: bingHeng
 * @Date 2019.03.23
 * @Version: 1.0
*/
public class StudentTest {

    public static void main(String[] args) {

        List<Student> studentList = new ArrayList<>();
        Student student1 = new Student(1, "张三", 1, 1, 10);
        Student student2 = new Student(2, "李四", 2, 1, 20);
        Student student3 = new Student(3, "王五", 1, 2, 30);
        Student student4 = new Student(4, "赵六", 2, 2, 40);
        Student student5 = new Student(5, "张七", 1, 3, 50);
        Student student6 = new Student(6, "吴八", 1, 4, 60);
        Student student7 = new Student(7, "冰烆", 1, 5, 70);
        Student student8 = new Student(8, "杨宇", 1, 6, 80);
        Student student9 = new Student(8, "杨宇", 1, 6, 80);

        studentList.add(student1);
        studentList.add(student2);
        studentList.add(student3);
        studentList.add(student4);
        studentList.add(student5);
        studentList.add(student6);
        studentList.add(student7);
        studentList.add(student8);
        studentList.add(student9);

        /**
         * 去重：
         *      去除重复对象（每个属性的值都一样），需要注意的是要先重写对象的equals和hashCode方法
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        System.out.println(studentList.size());
        List<Student> collect = studentList.stream().distinct().collect(Collectors.toList());
        System.out.println(collect.size());

        /**
         * 按照Id字段进行升序排序
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        List<Student> collect1 = studentList.stream().sorted(Comparator.comparingInt(Student::getId)).collect(Collectors.toList());
        System.out.println(collect1);

        /**
         *
         * 按照Id字段进行降序排序
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        List<Student> collect2 = studentList.stream().sorted((o1, o2) -> o2.getId() - o1.getId()).collect(Collectors.toList());
        System.out.println(collect2);

        /**
         * 过滤数据，按照自己的需求来筛选自己的数据，不及格的人
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        List<Student> collect3 = studentList.stream().filter(o -> o.getScore() < 60).collect(Collectors.toList());
        System.out.println(collect3);

        /**
         * 提取对象中的某一个元素
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        List<String> collect4 = studentList.stream().map(Student::getName).collect(Collectors.toList());
        System.out.println(collect4);

        /**
         * 统计所有人分数的和
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        double sum = studentList.stream().mapToDouble(Student::getScore).sum();
        System.out.println(sum);

        /**
         * 分组：按照字段中的某个属性，将List进行分组
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        Map<Integer, List<Student>> collect5 = studentList.stream().collect(Collectors.groupingBy(Student::getGrade));
        System.out.println(collect5);

        /**
         * 多重分组：先按照年级分组，再按照班级进行分组
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        Map<Integer, Map<Integer, List<Student>>> collect6 = studentList.stream().collect(Collectors.groupingBy(Student::getGrade, Collectors.groupingBy(Student::getClasses)));
        System.out.println(collect6);

        /**
         * stream是链式的，这些功能可以一起使用，例如：计算每个年级，每个班的及格人数
         */
        System.out.println("=========================================================我是华丽分割线=========================================================");
        Map<Integer, Map<Integer, List<Student>>> collect7 = studentList.stream().filter(o -> o.getScore() >= 60).collect(Collectors.groupingBy(Student::getGrade, Collectors.groupingBy(Student::getClasses)));
        System.out.println(collect7);

    }
}
